package com.powergisol.gis.timer;

import com.powergisol.gis.dao.TabServiceMapper;
import com.powergisol.gis.entity.TabService;
import com.powergisol.gis.vo.TabServiceVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

/**
 * Date: 2019/8/6  17:20
 * Description:
 */
@Component
public class ServiceStateTimer {
    private static Logger logger = LoggerFactory.getLogger(ServiceStateTimer.class);

    @Autowired
    private TabServiceMapper tabServiceMapper;
    @Value("${collectionName}")
    private String collectionName;

    @Scheduled(initialDelay = 5000, fixedRate = 300000)
    public void upServieState(){
        List<TabServiceVo> serviceList = tabServiceMapper.disAbledTabService();
        int i = 0;
        for (TabServiceVo tabService:serviceList) {
            String fileUrl = tabService.getFileUrl();
            if(null == fileUrl){
                upServiceState(tabService);
                i++;
                continue;
            }
            if(tabService.getServiceType()!=1 && tabService.getServiceType()!=4){
                continue;
            }
            File file = new File(fileUrl);
            if(!file.exists()){
                upServiceState(tabService);
                i++;
                continue;
            }
        }
        logger.info("定时器修改服务为不可用条数：{}",i);
    }
    private void upServiceState(TabServiceVo tabService){
        TabService service = new TabService();
        service.setState(3);
        service.setId(tabService.getId());
        tabServiceMapper.updateByPrimaryKeySelective(service);
    }

   
}
